package cn.skyquant.quant4j.service.boot.model.order;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.order.AccountOrderHisDTO;
import cn.skyquant.quant4j.api.order.OrderService;
import cn.skyquant.quant4j.service.boot.model.account.Account;
import cn.skyquant.quant4j.service.boot.model.account.AccountRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;

@Service("OrderServiceImpl")
public class OrderServiceImpl implements OrderService {

    @Resource
    AccountRepository accountRepository;
    @Resource
    AccountOrderHisRepository accountOrderHisRepository;

    @Override
    public ResultDTO<Long> getMaxDate(String accountCode, BorkerType borkerType, String symbol) {
        Optional<Account> accountOptional = accountRepository.findByCodeAndBorkerType(accountCode,borkerType);
        if(accountOptional.isPresent()){
            Account account = accountOptional.get();
            Date maxDate = accountOrderHisRepository.findMaxCloseTimeByAccountAndSymbol(account,symbol);
            if(maxDate!=null){
                return ResultDTO.success(maxDate.getTime());
            }
        }
        return ResultDTO.success(0L);
    }

    @Override
    public ResultDTO<Void> insert(AccountOrderHisDTO accountOrderHisDTO) {
        if(accountOrderHisDTO==null || accountOrderHisDTO.id==null || accountOrderHisDTO.borkerType==null || accountOrderHisDTO.accountId==null){
            return ResultDTO.error1;
        }
        Optional<Account> accountOptional = accountRepository.findByCodeAndBorkerType(accountOrderHisDTO.accountId,accountOrderHisDTO.borkerType);
        if(accountOptional.isPresent()){
            Account account = accountOptional.get();
            Optional<AccountOrderHis> optional = accountOrderHisRepository.findById(accountOrderHisDTO.id);
            if(!optional.isPresent()){
                AccountOrderHis accountOrderHis =  convertDTOToDO(accountOrderHisDTO);
                accountOrderHis.account = account;
                accountOrderHisRepository.save(accountOrderHis);
            }
        }

        return ResultDTO.success;
    }

    private static AccountOrderHis convertDTOToDO(AccountOrderHisDTO accountOrderHisDTO){
        AccountOrderHis accountOrderHis = new AccountOrderHis();
        accountOrderHis.id = accountOrderHisDTO.id;
        accountOrderHis.label = accountOrderHisDTO.label;
        accountOrderHis.amount = accountOrderHisDTO.amount;
        accountOrderHis.comment = accountOrderHisDTO.comment;
        accountOrderHis.command = accountOrderHisDTO.command;
        accountOrderHis.openPrice = accountOrderHisDTO.openPrice;
        accountOrderHis.closePrice = accountOrderHisDTO.closePrice;
        accountOrderHis.sl = accountOrderHisDTO.sl;
        accountOrderHis.tp = accountOrderHisDTO.tp;
        accountOrderHis.creationTime = new Date(accountOrderHisDTO.creationTime);
        accountOrderHis.fillTime = new Date(accountOrderHisDTO.fillTime);
        accountOrderHis.closeTime = new Date(accountOrderHisDTO.closeTime);
        accountOrderHis.commission = accountOrderHisDTO.commission;
        accountOrderHis.profit = accountOrderHisDTO.profit;
        accountOrderHis.symbol = accountOrderHisDTO.symbol;
        return accountOrderHis;
    }
}
